package com.itboye.textileexpres.myset;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.ref.WeakReference;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.MainApplication;
import com.itboye.textileexpres.base.SpPublicParams;
import com.itboye.textileexpres.main.MainActivity;
import com.itboye.textileexpres.util.SPUtils;

public class ActivityLanguage extends BaseActivity implements OnClickListener {
	ImageView back;
	ImageView img_other;
	TextView txt_save;
	TextView title;
	String wap = "http://api.guannan.itboye.com/public/index.php/Lang/support";
	String read;
	String jason;
	List<String> nameList = new ArrayList<String>();
	List<String> valeList = new ArrayList<String>();
	LinearLayout auto;
	String type;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_language;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		auto = (LinearLayout) findViewById(R.id.auto);
		img_other.setVisibility(View.GONE);
		title.setText("语言");
		txt_save.setVisibility(View.GONE);
		StringRequest stringRequest = new StringRequest(wap,
				new Listener<String>() {

					@Override
					public void onResponse(String arg0) {
						// TODO Auto-generated method stub
						JSONArray array;
						try {
							array = new JSONArray(arg0);
							for (int i = 0; i < array.length(); i++) {
								JSONObject object2 = array.getJSONObject(i);
								String name = object2.getString("name");
								String vale = object2.getString("value");
								nameList.add(name);
								valeList.add(vale);
							}
							change();
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					}
				}, new ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError arg0) {
						// TODO Auto-generated method stub

					}
				});

		MainApplication.addRequest(stringRequest);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.auto_text:
			type = "auto";
			break;
		case R.id.txt_save:
			SPUtils.put(ActivityLanguage.this, null, SpPublicParams.Language,
					type);
			restartActivity();
			break;
		default:
			break;
		}
	}

	void change() {
		for (int i = 0; i < nameList.size(); i++) {
			LayoutInflater inflater = LayoutInflater
					.from(ActivityLanguage.this);
			final View view = inflater.inflate(R.layout.item_language, null);
			TextView text = (TextView) view.findViewById(R.id.text);
			final CheckBox checkBox = (CheckBox) view
					.findViewById(R.id.checkbox);
			text.setText(nameList.get(i) + "");
			auto.addView(view);
			view.setTag(i);
			view.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					checkBox.setChecked(true);
					// SPUtils.put(ActivityLanguage.this, null,
					// SpPublicParams.Language,
					// valeList.get(Integer.parseInt(v.getTag() + "")));
					type = valeList.get(Integer.parseInt(v.getTag() + ""));
					view.setBackgroundColor(getResources().getColor(
							R.color.green));
					SPUtils.put(ActivityLanguage.this, null,
							SpPublicParams.Language, type);
					changeLanguage(type);
					restartActivity();
				}
			});
		}

	}

	private void changeLanguage(String sta) {
		// TODO Auto-generated method stub
		if (sta.equals("zh-cn")) {
			Locale.setDefault(Locale.CHINA);
			Configuration config = getBaseContext().getResources()
					.getConfiguration();
			config.locale = Locale.CHINA;
			getBaseContext().getResources().updateConfiguration(config,
					getBaseContext().getResources().getDisplayMetrics());
		} else if (sta.equals("zh-tw")) {
			Locale.setDefault(Locale.TAIWAN);
			Configuration config = getBaseContext().getResources()
					.getConfiguration();
			config.locale = Locale.TAIWAN;
			getBaseContext().getResources().updateConfiguration(config,
					getBaseContext().getResources().getDisplayMetrics());
		} else if (sta.equals("ja")) {
			Locale.setDefault(Locale.JAPAN);
			Configuration config = getBaseContext().getResources()
					.getConfiguration();
			config.locale = Locale.JAPAN;
			getBaseContext().getResources().updateConfiguration(config,
					getBaseContext().getResources().getDisplayMetrics());

		} else {
			Locale.setDefault(Locale.ENGLISH);
			Configuration config = getBaseContext().getResources()
					.getConfiguration();
			config.locale = Locale.ENGLISH;
			getBaseContext().getResources().updateConfiguration(config,
					getBaseContext().getResources().getDisplayMetrics());

		}

	}

	private void restartActivity() {
		// TODO Auto-generated method stub
		exit();
		final Intent intent = getPackageManager().getLaunchIntentForPackage(
				getPackageName());
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);

	}

	public void exit() {
		for (WeakReference<Activity> activity : MainApplication.activitys) {
			if (activity.get() != null) {
				activity.get().finish();
			}

		}

		// System.exit(0);

	}
}
